﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using com.huigo.Framework.Model;
using com.loto.Common.DBUtility;

namespace com.huigo.Framework.BLL
{
    public class PolicyTravel
    {
        public static List<Model.PolicyInfoTravel> GetPolicyList(string strPolicy, decimal unitPrice, out decimal totalPrice)
        {
            totalPrice = 0;
            //拆解保单信息
            string[] policyList = strPolicy.Split(new string[] { "||||" }, StringSplitOptions.RemoveEmptyEntries);
            //拆解保单信息
            List<Model.PolicyInfoTravel> infoList = new List<Model.PolicyInfoTravel>();
            Model.PolicyInfoTravel p = new Model.PolicyInfoTravel();
            foreach (string s in policyList)
            {
                string[] policy = s.Split(new string[] { "^^^^" }, StringSplitOptions.None);
                if (policy.Length == 15)
                {
                    p = new Model.PolicyInfoTravel();
                    p.PolicyOutOrderNo = DateTime.Now.ToString("yyMMdd") + Others.GetIndex();
                    p.PolicyUnitPrice = unitPrice;
                    p.PolicyHolderName = policy[0];
                    p.PolicyHolderCredentialType = policy[1];
                    p.PolicyHolderCredentialNo = policy[2];
                    p.PolicyHolderBirth = Convert.ToDateTime(policy[3]);
                    p.PolicyHolderGender = policy[4];
                    p.PolicyInsuredName = policy[5];
                    p.PolicyInsuredCredentialType = policy[6];
                    p.PolicyInsuredCredentialNo = policy[7];
                    p.PolicyInsuredBirth = Convert.ToDateTime(policy[8]);
                    p.PolicyInsuredGender = policy[9];
                    p.PolicyInsuredTel = policy[10];
                    p.PolicyFromPeriod = Convert.ToDateTime(policy[11]);
                    p.PolicyCount = Convert.ToInt16(policy[12]);
                    p.PolicyTravelLine = policy[13];
                    p.PolicyInsuredEmail = policy[14];
                    totalPrice += Convert.ToInt16(policy[12]) * unitPrice;
                    infoList.Add(p);
                }
            }
            return infoList;
        }

        public static int InsertPolicy(List<Model.PolicyInfoTravel> policyList, long custId, long orderId, long insType, SqlTransaction trans)
        {
            bool isSuc = false;
            foreach (Model.PolicyInfoTravel travel in policyList)
            {
                travel.PolicyOrderFid = orderId;
                travel.PolicyCustFid = custId;
                travel.PolicyStatus = (int)Dict.PolicyStatus.Uninsured;
                travel.PolicyFeeStatus = (int)Dict.PolicyFeeStatus.UnPay;
                long policyId = PolicyInfoTravel.InsertPolicyInfoTravel(travel, trans);
                if (!BLL.PolicyFlow.AddPolicyFlow(insType, policyId, 0, (int)Dict.PolicyStatus.Uninsured, com.loto.Common.DictEnum.GetDictValueByKey(Dict.DictPolicyStatus, (int)Dict.PolicyStatus.Uninsured), trans))
                {
                    isSuc = false;
                    break;
                }
                else
                {
                    isSuc = true;
                }
            }
            return isSuc ? policyList.Count : 0;
        }

        public static Model.PolicyInfoTravel PolicyIsInsuredSuc(long policyId)
        {
            Model.PolicyInfoTravel travel = new Model.PolicyInfoTravel();
            travel.PolicyId = policyId;
            travel.PolicyStatus = (int)Dict.PolicyStatus.InsuredSuc;
            List<string> fields = new List<string>() 
             {  
                "POLICY_NO",
                "POLICY_OUT_ORDER_NO",
                "POLICY_TRADE_ORDER_NO",
                "ORDER_NO",
                "ORDER_PRODUCT_CODE",
                "ORDER_INS_TYPE_FID"
             };
            List<JoinStatement> joinList = new List<JoinStatement>()
            {
                new JoinStatement("INNER","[HG_ORDER_INFO]","ORDER_ID=POLICY_ORDER_FID")
            };
            return SqlServerDal.PolicyInfoTravel.GetItem(travel, fields, joinList, null);
        }

    }
}
